package beans;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import entities.GroupEnum;
import entities.TransportEntity;
import entities.ExcursionEntity;
import entities.ResidenceEntity;
import dto.TransportDTO;
import dto.ResidenceDTO;
import dto.ExcursionDTO;
import beansInterfaces.BasicProductMgr;





import javax.annotation.security.RolesAllowed;
import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class BasicProductMgrBean
 */
@Stateless
public class BasicProductMgrBean implements BasicProductMgr {

	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
	

	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void addTransport(TransportDTO transportDTO) 
	{	
		em.persist(new TransportEntity(transportDTO));
	}


	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void addResidence(ResidenceDTO residenceDTO)
	{	
		em.persist(new ResidenceEntity(residenceDTO));
	}

	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void addExcursion(ExcursionDTO excursionDTO) 
	{	
		em.persist(new ExcursionEntity(excursionDTO));
	}

	
	
	
	
	
	
	
	
	
	
	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void updateTransport(TransportDTO transportDTO) {
		if(transportDTO.getDisponibility()==0)
		{
			transportDTO.setAvailable(false);
		}
		em.merge(new TransportEntity(transportDTO));
	}


	@Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void updateResidence(ResidenceDTO residenceDTO) {
		if(residenceDTO.getDisponibility()==0)
		{
			residenceDTO.setAvailable(false);
		}
		em.merge(new ResidenceEntity(residenceDTO));
		
	}
	@Override
	@RolesAllowed({GroupEnum._ADMIN})

	public void updateExcursion(ExcursionDTO excursionDTO) {
		if(excursionDTO.getDisponibility()==0)
		{
			excursionDTO.setAvailable(false);
		}
		em.merge(new ExcursionEntity(excursionDTO));
	}
	
	//FOR ADMIN
	public List<TransportEntity> getAllTransports() {
    	return em.createNamedQuery(TransportEntity.FIND_ALL, TransportEntity.class)
                .getResultList();
    }
    
    public List<ExcursionEntity> getAllExcursions() {
    	return em.createNamedQuery(ExcursionEntity.FIND_ALL, ExcursionEntity.class)
                .getResultList();
    }  
	
    private List<ResidenceEntity> getAllResidences() {
    	return em.createNamedQuery(ResidenceEntity.FIND_ALL, ResidenceEntity.class)
    			.getResultList();
    }
	
	//FOR USER
    
    public List<TransportEntity> getTransportForUser(){
    	return em.createNamedQuery(TransportEntity.FIND_FOR_USER, TransportEntity.class).getResultList();
    }

    public List<ExcursionEntity> getExcursionForUser(){
    	return em.createNamedQuery(ExcursionEntity.FIND_FOR_USER, ExcursionEntity.class).getResultList();
    }
    
    public List<ResidenceEntity> getResidenceForUser(){
    	return em.createNamedQuery(ResidenceEntity.FIND_FOR_USER, ResidenceEntity.class).getResultList();
    }
    	
    @Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void deleteTransport(TransportDTO transportDTO) {
		if(transportDTO!=null)
		{
			TransportEntity t = em.merge(new TransportEntity(transportDTO));
	    	em.remove(t);
		}

		
	}


    @Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void deleteResidence(ResidenceDTO residenceDTO) 
    {
    	if(residenceDTO !=null)
    	{
    		ResidenceEntity r = em.merge(new ResidenceEntity(residenceDTO));
    		
    		em.remove( r);
    	}
    	
		
	}


    @Override
	@RolesAllowed({GroupEnum._ADMIN})
	public void deleteExcursion(ExcursionDTO excursionDTO) {
    	
    	if(excursionDTO!=null)
    	{
    		ExcursionEntity e = em.merge(new ExcursionEntity(excursionDTO));
    		em.remove(e);
    	}
	}
	
	
	  public ExcursionDTO convertToDTO(ExcursionEntity excursion) {
			ExcursionDTO excursionDTO = new ExcursionDTO();
			
			excursionDTO.setId(excursion.getId());
			
			
			excursionDTO.setAvailable(excursion.isAvailable());
			excursionDTO.setDisponibility(excursion.getDisponibility());
			excursionDTO.setPrice(excursion.getPrice());
			excursionDTO.setDate(excursion.getDate());
			excursionDTO.setDescription(excursion.getDescription());
			excursionDTO.setType(excursion.getType());
			excursionDTO.setStartTime(excursion.getStartTime());
			excursionDTO.setEndTime(excursion.getEndTime());
			return excursionDTO;
		}

	  public TransportDTO convertToDTO(TransportEntity transport) {
			TransportDTO transportDTO = new TransportDTO();
			
			transportDTO.setId(transport.getId());
			
			
			
			transportDTO.setAvailable(transport.isAvailable());
			transportDTO.setDisponibility(transport.getDisponibility());
			transportDTO.setPrice(transport.getPrice());
			transportDTO.setClassNumber(transport.getClassNumber());
			transportDTO.setPassengers(transport.getPassengers());
			transportDTO.setType(transport.getType());
			transportDTO.setDepartureDate(transport.getDepartureDate());
			transportDTO.setFlightId(transport.getFlightId());
			transportDTO.setFromPlace(transport.getFromPlace());
			transportDTO.setToPlace(transport.getToPlace());
			transportDTO.setDepartureTime(transport.getDepartureTime());
			transportDTO.setCompany(transport.getCompany());
	
			return transportDTO;
		}
	  
	  public ResidenceDTO convertToDTO(ResidenceEntity residence) {
			ResidenceDTO residenceDTO = new ResidenceDTO();
			
			residenceDTO.setId(residence.getId());
			
			
			
			residenceDTO.setAvailable(residence.isAvailable());
			residenceDTO.setDisponibility(residence.getDisponibility());
			residenceDTO.setPrice(residence.getPrice());
			
			residenceDTO.setAddress(residence.getAddress());
			residenceDTO.setBeds(residence.getBeds());
			residenceDTO.setCity(residence.getCity());
			residenceDTO.setCountry(residence.getCountry());
			residenceDTO.setFromDate(residence.getFromDate());
			residenceDTO.setPhoneNumber(residence.getPhoneNumber());
			residenceDTO.setResidenceName(residence.getResidenceName());
			residenceDTO.setRooms(residence.getRooms());
			residenceDTO.setSite(residence.getSite());
			residenceDTO.setStars(residence.getStars());
			residenceDTO.setToDate(residence.getToDate());
			residenceDTO.setType(residence.getType());
			return residenceDTO;
	  }
	  
	  //FOR ADMIN
	  @Override
	  @RolesAllowed({GroupEnum._ADMIN, GroupEnum._USER})
	  public List<TransportDTO> getAllTransportsDTO()
	  {
		  Iterator<TransportEntity> iterator = getAllTransports().iterator();
		  List<TransportDTO> DTOList = new ArrayList<TransportDTO>();
		  
		  while(iterator.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator.next()));
		  }
		  return DTOList;
	  }
	  
	  @Override
	  @RolesAllowed({GroupEnum._ADMIN, GroupEnum._USER})
	  public List<ResidenceDTO> getAllResidencesDTO()
	  {
		  List<ResidenceEntity>  residences = getAllResidences();
		  List<ResidenceDTO> DTOList = new ArrayList<ResidenceDTO>();
		  
		  for(ResidenceEntity residence: residences)
		  {
			  ResidenceDTO dto = convertToDTO(residence);
			  DTOList.add(dto);
		  }
		  return DTOList;
	  }
	  
	  @Override
	  @RolesAllowed({GroupEnum._ADMIN, GroupEnum._USER})
	  public List<ExcursionDTO> getAllExcursionsDTO()
	  {
		  Iterator<ExcursionEntity> iterator = getAllExcursions().iterator();
		  List<ExcursionDTO> DTOList = new ArrayList<ExcursionDTO>();
		  
		  while(iterator.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator.next()));
			  
		  }
		  return DTOList;
	  }

	//FOR USER
	@Override
	@RolesAllowed({GroupEnum._USER, GroupEnum._ADMIN})
	public List<ExcursionDTO> getUserExcursionsDTO() {
		  Iterator<ExcursionEntity> iterator = getExcursionForUser().iterator();
		  List<ExcursionDTO> DTOList = new ArrayList<ExcursionDTO>();
		  
		  while(iterator.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator.next()));
			  
		  }
		  return DTOList;
	}


	@Override
	@RolesAllowed({GroupEnum._USER, GroupEnum._ADMIN})
	public List<TransportDTO> getUserTransportDTO() {
		  Iterator<TransportEntity> iterator = getTransportForUser().iterator();
		  List<TransportDTO> DTOList = new ArrayList<TransportDTO>();
		  
		  while(iterator.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator.next()));
			  
		  }
		  return DTOList;
	}


	@Override
	@RolesAllowed({GroupEnum._USER, GroupEnum._ADMIN})
	public List<ResidenceDTO> getUserResidenceDTO() {
		  Iterator<ResidenceEntity> iterator = getResidenceForUser().iterator();
		  List<ResidenceDTO> DTOList = new ArrayList<ResidenceDTO>();
		  
		  while(iterator.hasNext())
		  {
			  DTOList.add(convertToDTO(iterator.next()));
			  
		  }
		  return DTOList;
	}
	  
	  
}
